package com.macrounion.atv.controller.api;

import com.macrounion.atv.dto.req.BatchChannelReqDto;
import com.macrounion.atv.dto.req.ExchangeChannelReqDto;
import com.macrounion.atv.processor.ChannelProcessor;
import com.macrounion.atv.service.dto.req.ChannelReqDto;
import com.macrounion.atv.service.entity.Channel;
import com.macrounion.atv.service.entity.User;
import com.macrounion.atv.service.service.ChannelService;
import com.macrounion.base.annotation.Auth;
import com.macrounion.base.controller.api.BaseApiController;

import com.macrounion.base.controller.api.BaseRespAdviceController;
import com.macrounion.base.service.dto.BaseRespDto;
import com.macrounion.base.service.dto.PageRespDto;
import com.macrounion.base.service.exception.BusinessException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/channel")
public class ChannelApiController extends BaseApiController<Channel, Channel, ChannelProcessor, ChannelService, Channel,User> {

    @PostMapping("/updateTranslate")
    public BaseRespDto<String> updateTranslate(@RequestBody Channel channel){
        if(channel==null || StringUtils.isEmpty(channel.getId()) )
            throw new BusinessException(201,"参数错误");

        if(!StringUtils.isEmpty(channel.getTranslate()) && channel.getTranslate().length()>200)
            throw new BusinessException(202,"转译表达式不能超过200个字符");

        processor.updateTranslate(channel);
        return new BaseRespDto<>(200,"success");
    }

    @PostMapping("/exchangeChannel")
    public BaseRespDto<Boolean> exchangeChannel(@RequestBody ExchangeChannelReqDto channel){
         return processor.exchangeChannel(channel);
    }

    @PostMapping("/batchSaveChannel")
    public BaseRespDto<Boolean> batchSaveChannel(@RequestBody BatchChannelReqDto batchChannelReqDto){
        return processor.batchSaveChannel(batchChannelReqDto);
    }

    @ApiOperation("清空列表")
    @DeleteMapping({"/clear/{deviceId}"})
    @Auth(
            requireAuth = true,
            actionCode = {"delete"},
            writeLog = true
    )
    public Boolean clearChannelByDeviceId(@PathVariable String deviceId) {
        if(StringUtils.isEmpty(deviceId))
            throw new BusinessException(201,"未找到设备");
        return processor.clearChannelByDeviceId(deviceId);
    }

    @ApiOperation("初始化")
    @PostMapping("/initPull/{deviceId}")
    public Boolean initPull(@PathVariable String deviceId){
        if(StringUtils.isEmpty(deviceId))
            throw new BusinessException(201,"未找到设备");
        return processor.initPull(deviceId);
    }

//    @ApiOperation("添加虚拟通道")
//    @PostMapping("/addVirtual")
//    public Channel addVirtual(@RequestBody Channel channel){
//        if(channel==null)
//            throw new BusinessException(201,"参数有误");
//
//        return processor.addVirtual(channel);
//    }

    @ApiOperation("还原通道名")
    @PostMapping("/reductionChannelName/{deviceId}")
    public Boolean reductionChannelName(@PathVariable String deviceId){
        if(StringUtils.isEmpty(deviceId))
            throw new BusinessException(201,"未找到设备");

        return processor.reductionChannelName(deviceId);
    }

    @ApiOperation("根据设备ID得到协议通道列表")
    @GetMapping("/getChannelList")
    public List<Channel> getChannelList(String deviceId,String searchStr){
        if(StringUtils.isEmpty(deviceId))
            throw new BusinessException(201,"未找到设备");

        return processor.getChannelList(deviceId,searchStr);
    }

    @ApiOperation("")
    @PostMapping("/getPageChannelList")
    public PageRespDto<Channel> getPageChannelList(@RequestBody ChannelReqDto channelReqDto){
        return processor.getPageChannelList(channelReqDto);
    }

}